package com.ts.oss.service.impl;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import com.ts.common.RandomUtils;
import com.ts.oss.service.OssFileService;
import com.ts.oss.util.ConstantPropertiesUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Random;
import java.util.UUID;


@Service
public class OssServiceImpl implements OssFileService {
    @Override
    public String uploadFileAvatar(MultipartFile file) {
        String endpoint= ConstantPropertiesUtils.END_POINT;
        String accessKeyId=ConstantPropertiesUtils.KEY_ID;
        String accessKeySecret=ConstantPropertiesUtils.KEY_SECRET;
        String bucketName= ConstantPropertiesUtils.BUCKETNAME;
        //创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        try {
            InputStream inputStream = file.getInputStream();
            // 创建PutObjectRequest对象。
            //objectName 上传到oss文件路径和文件名称
            //获取文件名称
            String filename = file.getOriginalFilename();
            //修改文件名称 +随机值 防止被覆盖
            String s = UUID.randomUUID().toString().replaceAll("-","");
            filename=s+filename;
            //把文件按照日期进行分类
            //2019/11/12/0.1.jpg
            //获取当前的日期
            String s1 = new DateTime().toString("yyyy/MM/dd");
            //拼接
            filename=s1+"/"+filename;
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, filename, inputStream);
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            //https://mydemo-001.oss-cn-beijing.aliyuncs.com/123.jpg
            String url="https://"+bucketName+"."+endpoint+"/"+filename;
            return url;
        } catch (Exception e) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            return null;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }


    }

    @Override
    public String uploadByteFile(byte[] data) {
        String endpoint= ConstantPropertiesUtils.END_POINT;
        String accessKeyId=ConstantPropertiesUtils.KEY_ID;
        String accessKeySecret=ConstantPropertiesUtils.KEY_SECRET;
        String bucketName= ConstantPropertiesUtils.BUCKETNAME;
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        String s1 = new DateTime().toString("yyyy/MM/dd");
        String s2= RandomUtils.getSixBitRandom();
        String filename="example.png";
        String objectname=s1+"/"+s2+filename;
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectname, new ByteArrayInputStream(data));
            PutObjectResult result = ossClient.putObject(putObjectRequest);

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        String url = "https://" + bucketName + "." + endpoint + "/" + objectname;
        return url;
    }
}
